package org.cornland.systemWarning.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.cornland.systemWarning.model.PtInstance;

import java.util.List;

/**
 * <p>
 * 平台瞬量错误日志表 Mapper 接口
 * </p>
 *
 * @author Hawk Tang
 * @since 2021-10-13
*/

@Mapper
public interface PtInstanceMapper extends BaseMapper<PtInstance> {

    // 根据平台Id查询平台瞬量tag
    @Select(" SELECT distinct tag FROM well " +
            " left join djdwxx on well.id = djdwxx.well_id " +
            " where t_id = #{ptId} and px = 1 ")
    List<String> getPtInstanceTagsByPtId(@Param("ptId") int ptId);

    @Update(" update pt_instance set pt_instance.`flag` = 1 where id in ( " +
            " select t1.id from ( SELECT id FROM pt_instance " +
            " where t_id = #{tId} and flag = 0 and type = 1 ) t1) ")
    int updatePtInstanceAuto(@Param("tId") Integer tId);

    @Select("SELECT * FROM pt_instance " +
            "where t_id = #{tId} and type = #{type} " +
            "ORDER BY time desc " +
            "limit 1")
    PtInstance getlast(@Param("tId") Integer tId, @Param("type") Integer type);

    @Update("update pt_instance set flag = 1 where id = #{id}")
    int updateLastById(@Param("id") Integer id);

    @Select("SELECT * FROM pt_instance where t_id = #{tId} and type = #{type} and flag_number=2 limit 1")
    PtInstance getHave(@Param("tId") Integer tId, @Param("type") Integer type);

    @Select("SELECT * FROM pt_instance where t_id = #{tId} and type = #{type} and Flag=0 ORDER BY time DESC limit 1")
    PtInstance getData(@Param("tId") Integer tId, @Param("type") Integer type);
}
